package Interfaz;

import java.util.Vector;
import Modelo.Busqueda;
import Modelo.FileXML;
import Modelo.Ingredient;
import Modelo.Util;
import ingredientsClient.Consumer;
import java.io.File;

public class SearchGui extends javax.swing.JDialog {

    
    private Busqueda resultados ;
    private Vector tags = new Vector();
    private Vector exPath, exTitulo;
    
    public SearchGui(javax.swing.JFrame parent, boolean modal, Vector path, Vector titulo) {
        super(parent, modal);
        exPath = path;
        exTitulo = titulo;
        resultados = null;
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jListTags = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTxtTag = new javax.swing.JTextField();
        jBtnSearch = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListIngredients = new javax.swing.JList();
        jBtnGet = new javax.swing.JButton();
        jBtnCancel = new javax.swing.JButton();
        jBtnDelete = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Search ingredients");
        jListTags.setEnabled(false);
        jScrollPane1.setViewportView(jListTags);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("List of Tags Searched:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText("Enter a tag to search:");

        jTxtTag.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTxtTagKeyPressed(evt);
            }
        });

        jBtnSearch.setText("Search>>");
        jBtnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSearchActionPerformed(evt);
            }
        });

        jLabel4.setText("Results: (Ingredient's founded)");

        jListIngredients.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(jListIngredients);

        jBtnGet.setText("Download selected");
        jBtnGet.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGetActionPerformed(evt);
            }
        });

        jBtnCancel.setText("Close");
        jBtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelActionPerformed(evt);
            }
        });

        jBtnDelete.setText("Delete last");
        jBtnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                                .addGap(86, 86, 86))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jBtnDelete)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jBtnSearch))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
                                .addComponent(jTxtTag, javax.swing.GroupLayout.Alignment.LEADING)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(64, 64, 64)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jBtnGet)
                            .addComponent(jBtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTxtTag, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnDelete)
                    .addComponent(jBtnSearch)
                    .addComponent(jBtnGet))
                .addGap(28, 28, 28)
                .addComponent(jBtnCancel)
                .addContainerGap())
        );
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-461)/2, (screenSize.height-418)/2, 461, 418);
    }// </editor-fold>//GEN-END:initComponents

    private void jTxtTagKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtTagKeyPressed
        if(evt.getKeyChar() == '\n'){
            tags.addElement(jTxtTag.getText().toLowerCase());
            jListTags.setListData(tags);
            jTxtTag.setText("");
            jTxtTag.requestFocus();
        }
    }//GEN-LAST:event_jTxtTagKeyPressed

    private void jBtnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBtnCancelActionPerformed

    private void jBtnGetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGetActionPerformed
        
        int id = Integer.parseInt((String)resultados.getIds().get(jListIngredients.getSelectedIndex()));
        Consumer c = new Consumer();
        
        FileXML fxml =  FileXML.fromXml( c.getFile(id) );  //baja el FileXML desde el servidor
        
        Ingredient ing = Ingredient.fromXml(fxml.getXml()); //crea el objeto Ingrediente
        
        File file = new File( Util.getIngredientsDirectory());
        if(file.exists()){
        
            ing.save();
            
        }else{
            
            file = new File("ingredients");
                    
            if(file.mkdir()){
                ing.save();// "ingredients//"+fxml.getName() );
            }
        }
        
        dispose();
    }//GEN-LAST:event_jBtnGetActionPerformed

    private void jBtnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnDeleteActionPerformed
        tags.removeElementAt(tags.size()-1);
        
        
        jListTags.setListData(tags);
        jListTags.setSelectionInterval(0, tags.size()-1);
        
    }//GEN-LAST:event_jBtnDeleteActionPerformed

    private void jBtnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSearchActionPerformed
        
        Consumer c = new Consumer();
        
        this.resultados = c.busqueda(tags); 
        
        if(resultados !=null){
            try{
                jListIngredients.setListData(resultados.getTitulos());
            }catch(Exception e){
                    jListIngredients.setListData(new Vector());
            }
        } 
        else {
            Util.informDialog("No results", "No search results founded for provided tags ");
        }
        
    }//GEN-LAST:event_jBtnSearchActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancel;
    private javax.swing.JButton jBtnDelete;
    private javax.swing.JButton jBtnGet;
    private javax.swing.JButton jBtnSearch;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jListIngredients;
    private javax.swing.JList jListTags;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTxtTag;
    // End of variables declaration//GEN-END:variables
    
}
